<template>
 <el-dialog :title="isEdit?'编辑文件夹': '添加新文件夹'" :close-on-click-modal="false" 
  :visible.sync="show"  @submit.native.prevent
  class="dialog_del" :before-close="cancle" id="folder">
    <el-form :rules="rules" :model="form" ref="form">
      <p>请输入文件夹名称</p>
      <el-form-item prop="folder_name">
        <el-input placeholder="文件夹名称（必填项）" v-model="form.folder_name"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="search" @click="submitForm">确认</el-button>
      <el-button @click="cancle">取消</el-button>
    </span>
  </el-dialog>
 
</template>

<script>
import { folderAdd, folderEdit } from '@/api/gallery/gallery.js'
export default {
  data(){
    return{
      isEdit: false,
      curFolder: {},
      form: {
        folder_name: '',
      },
      rules:{ 
        folder_name:[
          { required: true, message: '文件夹名称为必填项', trigger: 'blur' },
          { min: 1,max: 12, message: '文件夹名称最多12字符', trigger: 'blur' },
        ],
      }
    }
  },
  props: {
    show: Boolean,
    closeDialog:Function,
    idList: Array
  },
  watch:{
    show(){
      if(this.show){
        if(this.isEdit){
          let curFolder = this.$parent.curFolder
          this.curFolder = curFolder
          this.form.folder_name = curFolder.label
        }else{
          this.form.folder_name = ''
        }
      }
    }
  },
  methods: {
    cancle() {
      this.$emit('closeDialog')
    },
    submitForm(){
       this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.isEdit){
            this.editForm()
          }else{
            this.addForm()
          }
         
        } 

      })
    },
    addForm () {
      let params = this.form
      folderAdd(params).then(res=>{
        console.log(res)
        if(res.state ==1){
          this.$message.success('添加成功')
          this.cancle()
        }
      })

    },
    editForm () {
      // 编辑文件夹
      let params = this.form
      params.id = this.curFolder.id
      folderEdit(params).then(res=>{
        console.log(res)
        if(res.state ==1){
          this.$message.success('编辑成功')
        }
        this.cancle()
      })
    }
  }
}
</script>

<style>

</style>